﻿using System;
using System.IO;
using Test;
using Artemis.Core;
using Artemis.Data.CacheProvider;
using Artemis.Data.CacheProvider.Caching;
 
namespace EFProviderWrapperDemo
{
    public partial class ExtendedNorthwindEntities : NorthwindEFEntities
    {
        private TextWriter logOutput;

        public ExtendedNorthwindEntities()
            : this("name=NorthwindEFEntities2")
        {
        }

        public ExtendedNorthwindEntities(string connectionString)
            : base(EntityConnectionWrapperUtils.CreateEntityConnectionWithWrappers(
                    connectionString,
                    "Artemis.Data.ProxyProvider",//"EFTracingProvider",
                    "Artemis.Data.CacheProvider"
            ))
        {

        }

        //#region Tracing Extensions

        //private EFTracingConnection TracingConnection
        //{
        //    get { return this.UnwrapConnection<EFTracingConnection>(); }
        //}

        //public event EventHandler<CommandExecutionEventArgs> CommandExecuting
        //{
        //    add { this.TracingConnection.CommandExecuting += value; }
        //    remove { this.TracingConnection.CommandExecuting -= value; }
        //}

        //public event EventHandler<CommandExecutionEventArgs> CommandFinished
        //{
        //    add { this.TracingConnection.CommandFinished += value; }
        //    remove { this.TracingConnection.CommandFinished -= value; }
        //}

        //public event EventHandler<CommandExecutionEventArgs> CommandFailed
        //{
        //    add { this.TracingConnection.CommandFailed += value; }
        //    remove { this.TracingConnection.CommandFailed -= value; }
        //}

        //private void AppendToLog(object sender, CommandExecutionEventArgs e)
        //{
        //    if (this.logOutput != null)
        //    {
        //        this.logOutput.WriteLine(e.ToTraceString().TrimEnd());
        //        this.logOutput.WriteLine();
        //    }
        //}

        //public TextWriter Log
        //{
        //    get { return this.logOutput; }
        //    set
        //    {
        //        if ((this.logOutput != null) != (value != null))
        //        {
        //            if (value == null)
        //            {
        //                CommandExecuting -= AppendToLog;
        //            }
        //            else
        //            {
        //                CommandExecuting += AppendToLog;
        //            }
        //        }

        //        this.logOutput = value;
        //    }
        //}


        //#endregion

        #region Caching Extensions

        private CachingConnection CachingConnection
        {
            get { return this.UnwrapConnection<CachingConnection>(); }
        }

        public ICache Cache
        {
            get { return CachingConnection.Cache; }
            set { CachingConnection.Cache = value; }
        }

        public CachingPolicy CachingPolicy
        {
            get { return CachingConnection.CachingPolicy; }
            set { CachingConnection.CachingPolicy = value; }
        }

        #endregion
    }


    public partial class ExtendedMySqlEntities:northwindEntities {

        public ExtendedMySqlEntities()
            : this("name=mysql")
        {
        }

        public ExtendedMySqlEntities(string connectionString)
            : base(EntityConnectionWrapperUtils.CreateEntityConnectionWithWrappers(
                    connectionString,
                    "Artemis.Data.ProxyProvider"//"EFTracingProvider",
                    //,"Artemis.Data.CacheProvider"
            ))
        {

        }
    }
}
